﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SMSTasker
{
    public partial class fmTask : Form
    {
        private int smsSize = 0;
        public bool EditMode = true;

        public fmTask()
        {
            InitializeComponent();
            this.menuItem2.Text = Localizer.Instance.GetString("fmTask.Cancel");
            Localize();
        }

        private void Localize()
        {
            this.menuItem1.Text = Localizer.Instance.GetString("fmTask.Save");
            this.label1.Text = Localizer.Instance.GetString("fmTask.Add");
            this.cbPriority.Text = Localizer.Instance.GetString("fmTask.Important");
            this.cbDays.Text = Localizer.Instance.GetString("fmTask.Days");
            this.label2.Text = Localizer.Instance.GetString("fmTask.Descr");
            this.label3.Text = Localizer.Instance.GetString("fmTask.Category");
            this.label4.Text = Localizer.Instance.GetString("fmSender.CharCount"); ;
            this.Text = Localizer.Instance.GetString("fmTask.Text");
            this.miCopy.Text = Localizer.Instance.GetString("fmTask.miCopy");
            this.miCut.Text = Localizer.Instance.GetString("fmTask.miCut");
            this.miPaste.Text = Localizer.Instance.GetString("fmTask.miPaste");
            this.miCancel.Text = Localizer.Instance.GetString("fmTask.miCancel");
        }

        public fmTask(bool edit)
        {
            InitializeComponent();
            EditMode = edit;
            if (EditMode)
            {
                this.menuItem2.Text = Localizer.Instance.GetString("fmTask.Cancel");
            }
            else
            {
                this.menuItem2.Text = Localizer.Instance.GetString("fmTask.Delete");
            }
            Localize();
        }

        void cbDays_Click(object sender, System.EventArgs e)
        {
            edDays.Enabled = ((CheckBox)sender).Checked;
            RecountSize();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            if (EditMode)
            {
                this.DialogResult = DialogResult.Cancel;
            }
            else
            {
                this.DialogResult = DialogResult.Abort;
            }
            
        }

        private void miCopy_Click(object sender, EventArgs e)
        {
            MenuItem item = (MenuItem)sender;
            ContextMenu menu = (ContextMenu)item.Parent;
            TextBox edit = (TextBox)menu.SourceControl;

            Clipboard.SetDataObject(edit.SelectedText);
        }

        private void miPaste_Click(object sender, EventArgs e)
        {
            MenuItem item = (MenuItem)sender;
            ContextMenu menu = (ContextMenu)item.Parent;
            TextBox edit = (TextBox)menu.SourceControl;

            edit.SelectedText = (String)Clipboard.GetDataObject().GetData("Text");
        }

        private void miCut_Click(object sender, EventArgs e)
        {
            MenuItem item = (MenuItem)sender;
            ContextMenu menu = (ContextMenu)item.Parent;
            TextBox edit = (TextBox)menu.SourceControl;

            Clipboard.SetDataObject(edit.SelectedText);
            edit.SelectedText = "";
        }

        void edit_GotFocus(object sender, System.EventArgs e)
        {
            sip.Enabled = true;
        }

        void edit_LostFocus(object sender, System.EventArgs e)
        {
            sip.Enabled = false;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void edTask_TextChanged(object sender, EventArgs e)
        {
            String text = ((TextBox)sender).Text;
            if (text.Length < 1)
            {
                RecountSize();
                return;
            }
            foreach (Object obj in cbTasks.Items)
            {
                String str = (String)obj;
                if (str.IndexOf(text) == 0)
                {
                    ((TextBox)sender).Text = str;
                    ((TextBox)sender).SelectionStart = text.Length;
                    ((TextBox)sender).SelectionLength = str.Length - text.Length;
                    break;
                }
            }
            RecountSize();
        }

        private void cbTasks_SelectedIndexChanged(object sender, EventArgs e)
        {
            edTask.Text = ((ComboBox)sender).SelectedItem.ToString();
        }

        private void cbPriority_CheckedChanged(object sender, EventArgs e)
        {
            RecountSize();
        }

        private void RecountSize()
        {
            String message = "";
            if (cbPriority.Checked)
            {
                message += "!";
            }
            if (cbDays.Checked)
            {
                if (edDays.Value >= 0)
                {
                    message += "+" + edDays.Value.ToString();
                }
            }
            if (edCategory.Text.Length > 0)
            {
                message += "$" + edCategory.Text;
            }
            if (edNote.Text.Length > 0)
            {
                message += "#" + edNote.Text;
            }
            message += (message.Length > 0 ? " " : "") + edTask.Text;
            message += ".";
            UTF8Encoding utf8 = new UTF8Encoding();
            smsSize = utf8.GetByteCount(message); ;
            lbSize.Text = smsSize.ToString();
        }

        private void edDays_ValueChanged(object sender, EventArgs e)
        {
            RecountSize();
        }

        private void edNote_TextChanged(object sender, EventArgs e)
        {
            RecountSize();
        }

        private void edCategory_TextChanged(object sender, EventArgs e)
        {
            RecountSize();
        }
    }
}